import { salaryListUrl } from '../../../../api/individual'
import { post, formatDate } from '../../../../utils/index'

Page({
  data: {
    salary: {},
    isDown: 'down',
    isBack: false,
    params: {
      current: 1,
      size: 10,
      startDate: formatDate(new Date(), 'yyyyMM'),
      endDate: formatDate(new Date(), 'yyyyMM')
    },
    datePopup: false,
    date: formatDate(new Date(), 'yyyy年MM月'),
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      return `${value}月`
    }
  },
  async loadData () { // 获取工资条
    const { params } = this.data
    const { data: { rows } } = await post(salaryListUrl, params)
    const salary = rows.length ? rows[0] : {}
    this.setData({ salary })
  },
  toggle () { // 切换显示五险
    let { isDown } = this.data
    isDown = isDown === 'down' ? '' : 'down'
    this.setData({ isDown })
  },
  search () { // 自定义查询
    if (this.data.isBack) {
      wx.navigateBack({})
      return
    }
    wx.navigateTo({ url: '/pages/individual/work/search/search' })
  },
  dateClick () { // 时间弹窗
    this.setData({ datePopup: true })
  },
  dateChange ({ detail }) { // 选择时间日期
    const { params } = this.data
    const date = formatDate(new Date(detail), 'yyyy年MM月')
    const dateMonth = formatDate(new Date(detail), 'yyyyMM')
    params.startDate = dateMonth
    params.endDate = dateMonth
    this.setData({ date, params })
    this.loadData()
    this.dateClose()
  },
  dateClose () { // 关闭时间
    this.setData({ datePopup: false })
  },
  onLoad ({ timestamp }) {
    if (timestamp) {
      this.dateChange({ detail: Number(timestamp) })
      this.setData({ isBack: true })
      return
    }
    this.loadData()
  },
  onPullDownRefresh () {
    this.loadData()
  }
})